import java.util.Arrays;

public class MinDeletionSize {

    public static void main(String[] args) {
        System.out.println((253^3) & 2);
        MinDeletionSize minDeletionSize = new MinDeletionSize();
        String[] str = {"abx","agz","bgc","bfc"};
        System.out.println(minDeletionSize.minDeletionSize(str));
    }

    public int minDeletionSize1(String[] strs) {
        String[] temp = new String[strs.length];
        int ans = 0;
        for (int i = 0; i < strs[0].length(); i++) {
            String[] copy = Arrays.copyOf(temp, strs.length);
            for (int j = 0; j < strs.length; j++) {
                copy[j] += strs[j].charAt(i);
            }
            if (check(copy)) {
                temp = copy;
            } else ++ans;
        }
        return ans;
    }

    public boolean check(String[] str) {
        for (int i = 0; i < str.length - 1; i++) {
            if (str[i].compareTo(str[i + 1]) > 0) return false;
        }
        return true;
    }

    public int minDeletionSize(String[] strs) {
        int m = strs.length;
        int n = strs[0].length();
        boolean[] cut = new boolean[m - 1];
        int ans = 0;
        search: for (int i = 0; i < n; i++) {
            for (int j = 0; j < m - 1; j++) {
                if (!cut[j] && strs[j].charAt(i) > strs[j + 1].charAt(i)) {
                    ++ans;
                    continue search;
                }
            }
            for (int j = 0; j < m - 1; j++) {
                if (strs[j].charAt(i) < strs[j + 1].charAt(i)) cut[j] = true;
            }
        }
        return ans;
    }
}